﻿Option Explicit On
'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose: the pagesheet is used in the masters and the pages
'
'*******************************************************************


#Region " imports "

Imports System
Imports System.Object
Imports System.IO
Imports System.IO.TextReader
' need to reference windowsbase in .net
Imports System.IO.Packaging

' need for dbconnectionstringbuilder used in clsConnections
Imports System.Data.Common

Imports System.Windows
Imports System.Windows.Forms
Imports System.Text.StringBuilder

Imports Microsoft.VisualBasic

Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports System.Xml
Imports System.Linq
Imports System.Linq.Enumerable
Imports System.Windows.Input

Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports ADODB
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


' visio v15 does not use a xName prefix which may put it into conflict with other open office apps
Imports <xmlns="http://schemas.microsoft.com/office/visio/2012/main">
Imports <xmlns:rs="urn:schemas-microsoft-com:rowset">

' generic namespace for office open xml, used in all of them
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">


Imports pkgVisio.clsVisPackage
Imports pkgVisio.clsStructures

#End Region



'**********************************************************************************
'
'**********************************************************************************
'



Public Class frmMasters


#Region " Properties and Declarations "

    '
    ' this is the "Masters" file
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public m_tempDoc As XDocument
    Public Property tempDoc As XDocument
        Get
            Return Me.m_tempDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "tempDoc is Null.")
            End If
            Me.m_tempDoc = value
        End Set
    End Property


    '
    '
    '
    Public m_visPackage As clsVisPackage
    Public Property visPackage As clsVisPackage
        Get
            visPackage = Me.m_visPackage
        End Get
        Set(value As clsVisPackage)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "visPackage is Null.")
            End If
            Me.m_visPackage = value
        End Set
    End Property

    '
    '
    '
    Private m_visMasters As clsMasters
    Public Property visMasters As clsMasters
        Get
            visMasters = Me.m_visMasters
        End Get
        Set(value As clsMasters)
            Me.m_visMasters = value
        End Set
    End Property




#End Region


#Region " methods "



    '
    ' throw not implemented exception
    '
    Private Sub FutureFeature()
        Throw New NotImplementedException
    End Sub

    '    Try
    '        FutureFeature()
    '    Catch NotImp As NotImplementedException
    '        MsgBox(NotImp.Message)
    '    End Try


#End Region

#Region " form events "


    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub


    '
    '
    '
    Private Sub frmMasters_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Me.lvMasters.Clear()
        Me.lvMstrAttr.Clear()
        Me.lvMstrElem.Clear()
        Me.pbIcon.Image = Nothing


    End Sub


    '
    '
    '
    Private Sub frmMasters_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown


        Me.visPackage.nsDict.Clear()
        Me.visPackage.attDict.Clear()
        Me.visPackage.elemDict.Clear()

        Me.pbIcon.Image = Nothing
        Me.lvMasters.Clear()
        Me.lvMstrAttr.Clear()
        Me.lvMstrElem.Clear()


        If visMasters IsNot Nothing Then
            Me.m_visMasters = Nothing
            Me.m_visMasters = New clsMasters
        Else
            Me.m_visMasters = New clsMasters
        End If

        ' go find the masters.xml file and load it
        Me.visMasters.loadMastersXDoc(Me.visPackage)
        ' go find the individual shape files and load (mirror to a dictionary)
        Me.visMasters.loadMasters(Me.lvMasters)

    End Sub


    '
    '
    '
    Private Sub lvMasters_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvMasters.SelectedIndexChanged

        Try

            Dim mstrsIndices As ListView.SelectedIndexCollection = Me.lvMasters.SelectedIndices
            Dim mstrsItems As ListView.SelectedListViewItemCollection = Me.lvMasters.SelectedItems
            Dim mstrsItem As ListViewItem



            If 0 < mstrsItems.Count Then
                mstrsItem = mstrsItems(0)

                ' get the "master" summary data and load it into the xDocMaster
                Me.visMasters.loadMasterSummaryByMasterID(mstrsItem.Text)

                If Me.visMasters.xDocMaster IsNot Nothing Then

                    Me.visPackage.readAttAndElem(Me.visMasters.xDocMaster, "", "Master")
                    Me.visPackage.loadAttElemLV(Me.lvMstrAttr, Me.visPackage.attDict)
                    Me.visPackage.loadAttElemLV(Me.lvMstrElem, Me.visPackage.elemDict)

                    Dim blnFound As Boolean = False
                    Try
                        blnFound = Me.visPackage.elemDict.ContainsKey("Icon")
                    Catch err As System.Collections.Generic.KeyNotFoundException
                    End Try

                    If blnFound = True Then
                        Me.pbIcon.Image = Me.visMasters.convertIcon(Me.visMasters.xDocMaster)
                    End If

                End If ' test for master found
            End If ' test for selected


        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Private Sub btnMstrShp_Click(sender As System.Object, e As System.EventArgs) Handles btnMstrShp.Click


        Dim mstrsIndices As ListView.SelectedIndexCollection = Me.lvMasters.SelectedIndices
        Dim mstrsItems As ListView.SelectedListViewItemCollection = Me.lvMasters.SelectedItems
        Dim mstrsItem As ListViewItem

        Dim visMaster As clsMaster = New clsMaster

        Try

            If 0 < mstrsItems.Count Then
                mstrsItem = mstrsItems(0)
                Me.m_tempDoc = Nothing

                Dim strMstrFile As String = mstrsItem.SubItems(3).Text
                Dim strMstrName As String = mstrsItem.SubItems(0).Text

                visMaster.visPackage = Me.visPackage
                'get the master shape data and load it 
                visMaster.loadMasterShapeXDoc(strMstrFile)

                If visMaster.xDoc IsNot Nothing Then

                    ' retrieve the summary data from the local "masters" class
                    ' this should have been loaded when the master was selected
                    frmShape.strShpNameU = visMasters.dictMastersID(CLng(strMstrName)).mstNameU
                    frmShape.strShpName = visMasters.dictMastersID(CLng(strMstrName)).mstName


                    Me.visPackage.readAttAndElem(visMaster.xDoc, "", "Shape")
                    ' the shape form expects to have a current pageshape structure
                    Dim newPs As stcPageShape
                    With newPs
                        .psPage = -1
                        .psShape = Me.visPackage.attDict("ID")
                    End With

                    frmShape.initShapeData("Shape", Me.visPackage, visMaster.xDoc, newPs)
                    frmShape.ShowDialog(Me)
                    ' remove the visMaster local copy
                    visMaster = Nothing

                End If ' test that node was found
            End If ' test for selected master in listview


        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If visMaster IsNot Nothing Then
            visMaster = Nothing
        End If

    End Sub


    '
    '
    '
    Private Sub lvMstrElem_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvMstrElem.SelectedIndexChanged

        Try

            Dim mstrelemItems As ListView.SelectedListViewItemCollection = lvMstrElem.SelectedItems
            Dim mstrelemItem As ListViewItem
            Dim mstrelemSubItem As ListViewItem.ListViewSubItem

            If 0 < mstrelemItems.Count Then
                mstrelemItem = mstrelemItems(0)
                mstrelemSubItem = mstrelemItem.SubItems(0)

                Select Case LCase(mstrelemSubItem.Text)
                    Case "pagesheet"
                        Dim newPs As stcPageShape
                        With newPs
                            .psPage = -1
                            .psShape = -1
                        End With

                        frmShape.initShapeData("PageSheet", Me.visPackage, visMasters.xDocMaster, newPs)
                        frmShape.ShowDialog(Me)

                    Case Else
                        Try
                            FutureFeature()
                        Catch NotImp As NotImplementedException
                            MsgBox(NotImp.Message)
                        End Try
                End Select
            End If


        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub



#End Region



    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me._xDoc = Nothing
        Me.xDoc = New XDocument

        Me.m_visPackage = Nothing
        Me.visPackage = New clsVisPackage

        Me.m_visMasters = Nothing
        Me.visMasters = New clsMasters

    End Sub





End Class